<!-- 选择用户对话框 -->
<template>
  <el-dialog :title="title" :visible.sync="visible" width="800px" append-to-body @close="handleClose">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            node-key="id"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="用户名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户名称" align="center" prop="userName" />
          <el-table-column label="昵称" align="center" prop="nickName" />
          <el-table-column label="部门" align="center" prop="dept.deptName" />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listUser, deptTreeSelect } from "@/api/system/user";

export default {
  name: "SelectUserDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "选择用户",
    },
    multi: {
        type: Boolean,
        default: false
    }
  },
  data() {
    return {
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 用户表格数据
      userList: null,
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        deptId: undefined
      },
      // 选中数组
      selection: []
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    visible(newVal) {
      if (newVal) {
        // 对话框打开时，加载数据
        this.getDeptTree();
        this.getList();
      } else {
        // 对话框关闭时，重置状态
        this.resetQuery();
        this.selection = [];
      }
    }
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      // 使用setCurrentKey之前确保已经设置了node-key属性
      if (this.$refs.tree) {
        this.$refs.tree.setCurrentKey(null);
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
    },
    /**
     * @description: "取消"按钮触发，只负责通知父组件关闭对话框
     */
    handleClose() {
      this.$emit('update:visible', false);
    },
    /**
     * @description: "确定"按钮触发，负责校验数据、提交数据、并通知父组件关闭
     */
    handleConfirm() {
      if (this.selection.length === 0) {
        this.$modal.msgWarning("请至少选择一个用户");
        return;
      }
      if (!this.multi && this.selection.length > 1) {
          this.$modal.msgWarning("只能选择一个用户");
          return;
      }
      // 步骤1：向父组件提交选中的数据
      this.$emit('confirm', this.selection);
      // 步骤2：通知父组件关闭对话框
      this.$emit('update:visible', false);
    }
  }
};
</script>
